package com.billy.ohos.swipe.refresh;

import com.billy.ohos.swipe.ResourceTable;
import com.billy.ohos.swipe.SmartSwipe;
import com.billy.ohos.swipe.SmartSwipeRefresh;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * classic header for {@link SmartSwipeRefresh}
 * @author billy.qi
 */
public class ClassicHeader extends DependentLayout implements SmartSwipeRefresh.SmartSwipeRefreshHeader {
    public Text mTitleTextView;
    public Image mProgressImageView;
    public int mStrResId;
    public AnimatorValue  animator;
    private Component component;

    public ClassicHeader(Context context) {
        super(context);
    }

    public ClassicHeader(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    @Override
    public Component getView() {
        return component;
    }

    @Override
    public void onInit(boolean horizontal) {
        ComponentContainer.LayoutConfig layoutParams = getLayoutConfig();
        if (horizontal) {
            component = LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_ssr_classic_header_footer_horizontal, this , false);
            if (layoutParams == null) {
                int width = SmartSwipe.dp2px(60, getContext());
                layoutParams = new ComponentContainer.LayoutConfig(width, ComponentContainer.LayoutConfig.MATCH_PARENT);
            }
        } else {
            component = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_ssr_classic_header_footer,
                    this , false);
            if (layoutParams == null) {
                int height = SmartSwipe.dp2px(60, getContext());
                layoutParams = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, height);
            }
        }
        setLayoutConfig(layoutParams);
        mProgressImageView = (Image) component.findComponentById(ResourceTable.Id_ssr_classics_progress);
        mProgressImageView.setVisibility(VISIBLE);
        mTitleTextView = (Text) component.findComponentById(ResourceTable.Id_ssr_classics_title);
        mTitleTextView.setText(ResourceTable.String_ssr_header_pulling);
        animator = new AnimatorValue ();
        animator.setDuration(5000);
        animator.setLoopedCount(AnimatorValue.INFINITE);
        animator.setValueUpdateListener((animatorValue, v) -> {
            mProgressImageView.setRotation(v * 1800);
        });
    }

    public void cancelAnimation() {
        animator.cancel();
        mProgressImageView.setVisibility(HIDE);
    }

    public void showAnimation() {
        animator.start();
        mProgressImageView.setVisibility(VISIBLE);
    }

    @Override
    public void onStartDragging() {
    }

    @Override
    public void onProgress(boolean dragging, float progress) {
        if (dragging) {
            setText(progress >= 1 ? ResourceTable.String_ssr_header_release : ResourceTable.String_ssr_header_pulling);
        } else if (progress <= 0) {
            cancelAnimation();
        }
    }

    @Override
    public long onFinish(boolean success) {
        cancelAnimation();
        setText(success ? ResourceTable.String_ssr_header_finish : ResourceTable.String_ssr_header_failed);
        return 500;
    }

    @Override
    public void onReset() {
        //修复了:先下拉刷新完成(此时显示为刷新完成),然后调用 startRefresh 时,看到的不是"下拉刷新"而是"刷新完成"的问题.
        //fix:first pull to refresh finished(hint:Refresh Success),
        // then invoke startRefresh,now will allways see the 'Refresh Success'.so reset the text.
        setText(ResourceTable.String_ssr_header_pulling);
    }

    @Override
    public void onDataLoading() {
        showAnimation();
        setText(ResourceTable.String_ssr_header_refreshing);
    }

    public void setText(int strResId) {
        if (mStrResId != strResId && mTitleTextView != null) {
            mStrResId = strResId;
            mTitleTextView.setText(strResId);
        }
    }
}
